﻿using System.Linq;
using System.Windows.Controls;
using System.Collections.Generic;
using System.Collections.ObjectModel;

namespace XDCCManager.Popups
{
	public class UploadServiceTemplateSelector : DataTemplateSelector
	{
		private ObservableCollection<UploadServiceTemplateDefinition> definitions;
		public ObservableCollection<UploadServiceTemplateDefinition> Definitions { get { return definitions; } }

		public UploadServiceTemplateSelector()
		{
			definitions = new ObservableCollection<UploadServiceTemplateDefinition>();
		}

		public override System.Windows.DataTemplate SelectTemplate(object item, System.Windows.DependencyObject container)
		{
			UploadServiceTemplateDefinition noneDefinition = Definitions.FirstOrDefault((def) => { return def.Kind == Model.Servers.UploadServiceKind.None; });
			if (item == null)
				return (noneDefinition.Template != null) ? noneDefinition.Template : null;
			UploadServiceTemplateDefinition definition = Definitions.FirstOrDefault((def) => { return def.Type == item.GetType(); });
			return (definition != null) ? (definition.Template) : ((noneDefinition.Template != null) ? noneDefinition.Template : null);
		}
	}
}
